<template>
  <div :class="className"
       :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from "echarts"

export default {
  props: {
    className: {
      type: String,
      default: "chart"
    },
    headTitle: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "350px"
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Array,
      required: true
    },
    timeData: {
      type: Array,
      required: true
    },
    unit: {
      type: String,
      default: ""
    },
    color: {
      type: String,
      default: "#239DD2"
    }
  },
  data() {
    return {
      chart: null
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(
        );
      }
    }
  },
  mounted() {
    window.addEventListener("resize", this.resizeHandler);
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.resizeHandler);
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    resizeHandler() {
      this.chart.resize();
    },
    initChart() {
      this.chart = echarts.init(this.$el);
      this.setOptions(
      );
    },
    setOptions() {
      this.chart.setOption({
        color: ["#FFAE51", "#7FE47E", "#FF7171", "#FBF21E", "#36DDF3",
          "#A866FF", "#30AAFF"],
        tooltip: {
          trigger: 'item'
        },
        legend: {
          icon: "rect",
          orient: 'vertical',
          right: 'left',
          data: ['参数1', '参数2', '参数3', '参数4', '参数5', '参数6', '参数7']
        },
        series: [
          {
            // name: 'Access From',
            type: 'pie',
            radius: ['35%', '60%'],
            center: ['150', '50%'],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'center'
            },
            // toolbox: {
            //   show: true,
            //   feature: {
            //     mark: { show: true },
            //     dataView: { show: true, readOnly: false },
            //     restore: { show: true },
            //     saveAsImage: { show: true }
            //   }
            // },
            emphasis: {
              scale: true,
              scaleSize: 2,
              label: {
                show: true,
                fontSize: 16,
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: true
            },
            label: {
              show: true,
              position: "outside"
            },
            data: [{ name: "参数1", value: 1 }, { name: "参数2", value: 7 }, { name: "参数3", value: 8 }, { name: "参数4", value: 22 }, { name: "参数5", value: 10 }, { name: "参数6", value: 5 }, { name: "参数7", value: 3 }]
          }
        ]
      });

    }
  }
};
</script>
